package com.bidevalution.business.checkJob;

import com.alibaba.dubbo.common.json.JSON;
import com.bidevalution.business.exception.CheckException;
import com.bidevalution.business.factory.CheckJobBase;
import com.bidevalution.business.service.BeBidResultService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionWarnMsgService;
import com.bidevalution.business.vo.CheckJobVo;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionWarnMsg;
import com.bidevalution.dao.entity.CheckJob;
import com.bidevalution.dao.entity.ExpertWarnMsgVo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 判断排名是否合理
 * @Date        2020-03-03 17:19
 * @Author      lhl
 */
@Log4j
@Component
public class RankCheckJob extends CheckJobBase {

    @Autowired
    private BeBidResultService beBidResultService;
    @Autowired
    private BeSectionWarnMsgService beSectionWarnMsgService;
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;

    @Override
    public void check(CheckJob checkJob) throws Exception,CheckException {
        String sectionStepId = checkJob.getSectionStepId();
        List<ExpertWarnMsgVo> errorList = rankCheckJob.beBidResultService.getRankErrorInfo(sectionStepId);
        if (errorList.size() > 0) {
            Map<String, List<String>> notificationMap = new HashMap<>();
            // 初始全部专家
            for (BeSectionExpertInfo item : checkJob.getAllExperts()) {
                String expId = item.getSeqId();
                notificationMap.put(expId, new ArrayList<>());
            }

            // 幅值异常专家
            for (ExpertWarnMsgVo item: errorList) {
                notificationMap.get(item.getExpertId()).add(item.getEnterpriseId());
            }

            throw new CheckException("中标候选人排名存在一致", notificationMap,
                    "中标候选人排名存在一致，请组长检查！",
                    "中标候选人排名存在一致，必须等组长重新调整排名后才能下一步，请等待！");
        }
    }

    private static RankCheckJob rankCheckJob;

    @PostConstruct
    public void init() {
        rankCheckJob = this;
    }
}
